/*
 * Copyright (c) 2023, LXMS (lxinet.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.lxinet.lxms.system.controller;

import cn.lxinet.lxms.framework.core.controller.BaseController;
import cn.lxinet.lxms.framework.core.vo.Result;
import cn.lxinet.lxms.framework.security.helper.SecurityHelper;
import cn.lxinet.lxms.system.dto.sysUser.SysLoginDto;
import cn.lxinet.lxms.system.entity.SysUser;
import cn.lxinet.lxms.system.service.SysMenuService;
import cn.lxinet.lxms.system.service.SysUserRoleService;
import cn.lxinet.lxms.system.service.SysUserService;
import cn.lxinet.lxms.system.vo.SysMenuVo;
import cn.lxinet.lxms.system.vo.SysUserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 认证
 *
 * @author zcx
 * @date 2023/07/09
 */
@Slf4j
@Tag(name = "认证")
@RestController
@RequestMapping("/system/auth")
public class AuthController extends BaseController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("login")
    @Operation(summary = "账号密码登录")
    public Result login(@Valid @RequestBody SysLoginDto sysLoginDto){
        SysUserVo vo = sysUserService.login(sysLoginDto, request);
        return Result.success(vo);
    }

    @GetMapping("getUserInfo")
    @Operation(summary = "获取登录用户信息、权限列表")
    public Result getUserInfo(){
        SysUser sysUser = sysUserService.findById(SecurityHelper.getLoginUser().getId());
        SysUserVo sysUserVo = new SysUserVo(sysUser.getId(), sysUser.getName(), sysUser.getRealName(), sysUser.getAvatar());
        sysUserVo.setPermissions(SecurityHelper.getPermissions());
        return Result.success(sysUserVo);
    }

    @GetMapping("menuList")
    @Operation(summary = "获取菜单列表")
    public Result menuList(){
        List<SysMenuVo> menuVoList = sysMenuService.getUserMenuList();
        return Result.success(menuVoList);
    }


}